package com.ailight.smart.ai.strategy.impl;

import cn.hutool.json.JSONUtil;
import com.ailight.smart.ai.assembler.AiModelAssembler;
import com.ailight.smart.ai.enums.AiModelEnum;
import com.ailight.smart.ai.listener.CustomSseListener;
import com.ailight.smart.ai.strategy.AiModelStrategy;
import com.ailight.smart.ai.util.OkHttpUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.Map;

/**
 * @author: 朱海鹏
 * @Package: com.ailight.smart.ai.strategy.impl
 * @Project: ai-light
 * @date: 2025/3/19 10:30
 * @description :
 */
@Component
public class DeepSeekReasonerStrategy implements AiModelStrategy {

    @Value("${ai.model.deepseek.api_key}")
    private String key;

    @Override
    public <R> R getResultNoStream(String t, Class<R> r, AiModelEnum model) {
        // 拼接Map
        Map<String, String> map = AiModelAssembler.buideDpHeardMap(key, false);
        // 调用ok http
        String s = OkHttpUtil.doPost(map, model.getUrl(), t);
        if (s != null) {
            return JSONUtil.toBean(s, r);
        }
        return null;
    }

    @Override
    public SseEmitter getSseEmitter(String t, AiModelEnum model) {
        Map<String, String> map = AiModelAssembler.buideDpHeardMap(key, true);
        SseEmitter sseEmitter = new SseEmitter(60_000L);
        CustomSseListener customSseListener = new CustomSseListener(sseEmitter);
        OkHttpUtil.doPostByStream(map, model.getUrl(), t, customSseListener);
        return sseEmitter;
    }
}
